/**
 * COPYRIGHT© KMTC 2007
 * This software is the proprietary information of Korea Marine Transport Co.LTD
 * 
 * Revision History
 * Author			Date		Description
 * ------			----		-----------
 * KIMJUHEE@wees.co.kr		
 */
package kr.or.javacafe.common.util;
 
import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import kr.or.javacafe.common.exception.*;

/**
 * @author codeguru
 *
 */
public class DateUtil {
	
	/**
	 * ?��?�� ?��짜�?? Date Type ?���? 반환
	 * @return Date
	 */
	public static Date getToday(){
		return Calendar.getInstance().getTime();
	}
	
	/**
	 * ?��?�� ?��짜�?? String Type?���? 반환?��?�� Default 메소?��
	 * 반환 ?���? ?��?��?�� ?��?��??? "yyyyMMdd"
	 * @return Today String
	 */
	public static String getTodayStrByDefault(){
		
		return DateUtil.getDateByPattern(DateUtil.getToday(), "yyyyMMdd");
	}
	
	/**
	 * ?��?�� ?��짜�?? ?��?��?�� ?��?��?���? �??��?��?��  String Type?���? 반환?��?�� 메소?��
	 * @param pattern
	 * @return Today String
	 */
	public static String getTodayStrByPattern(String pattern){
		
		return DateUtil.getDateByPattern(DateUtil.getToday(), pattern);
	}
	
	/**
	 * ?��짜�?? ?��?��?�� ?��?��?���? �??��?��?�� String Type?���? 반환
	 * @param date ?��?��?��?�� ?���?
	 * @param pattern ?���? ?��?�� ?��?��
	 * @return Date String
	 */
	public static String getDateByPattern(Date date, String pattern){
	
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		
		return sdf.format(date);
	}
	
	/**
	 * 기�?? ?��짜�?? 기�?? Field ?�� ?���? 차�?? ?��?��?��?�� ?��로운 ?��짜�?? 반환
	 * @param date 기�?? ?���?
	 * @param field 기�?? Date Field
	 * @param diff ?���? �?
	 * @return Date
	 */
	public static Date getDateDiff(Date date, int field, int diff){
		
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(field, diff);
		
		return cal.getTime();
	}
	
	/**
	 * 기�?? ?��짜�?? 기�?? Field ?�� ?���? 차�?? ?��?��?��?�� ?��로운 ?��짜�?? String Type?���? 반환
	 * 반환 ?���? ?��?��?�� ?��?��??? "yyyyMMdd"
	 * @param date 기�?? ?���?
	 * @param field 기�?? Date Field
	 * @param diff ?���? �?
	 * @return Date String
	 */
	public static String getDateStrDiff(Date date, int field, int diff){
		
		return DateUtil.getDateByPattern( DateUtil.getDateDiff(date, field, diff), "yyyyMMdd");
	}
	
	/**
	 * 기�?? ?��짜�?? 기�?? Field ?�� ?���? 차�?? ?��?��?��?�� ?��로운 ?��짜�??, ?��?��?�� ?���? ?��?��?�� ?��?��?���? �??��?��?�� String Type?���? 반환
	 * @param date 기�?? ?���?
	 * @param field 기�?? Date Field
	 * @param diff ?���? �?
	 * @param pattern ?���? ?��?�� ?��?��
	 * @return Date String
	 */
	public static String getDateStrDiffByPattern(Date date, int field, int diff, String pattern){
		
		return DateUtil.getDateByPattern( DateUtil.getDateDiff(date, field, diff), pattern);
	}
	
	/**
	 * 기�?? ?���?(String)??? 기�?? Field ?�� ?���? 차�?? ?��?��?��?�� ?��로운 ?��짜�??, ?��?��?�� ?���? ?��?��?�� ?��?��?���? �??��?��?�� String Type?���? 반환
	 * @param date 기�?? ?���?
	 * @param field 기�?? Date Field
	 * @param diff ?���? �?
	 * @return Date String 
	 */
	public static String getDateStrDiffByPattern(String date, int field, int diff) {
			
		if (date.length() >= 8) {
			Calendar cal = Calendar.getInstance();
			int year = Integer.parseInt(date.substring(0, 4));
			int month = Integer.parseInt(date.substring(4, 6)) - 1;
			int day = Integer.parseInt(date.substring(6, 8));
			cal.set(year, month, day, 0, 0, 0);
			cal.add(field, diff);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			return sdf.format(cal.getTime());
		} else {
			return "-";
		}
	}
	/**
	 * String ?���? Source �? Date Type?���? �??��?��?�� 반환
	 * @param dateStr ?��?��?�� String Source
	 * @return Date
	 * @throws KmtcRuntimeException
	 */
	public static Date parseStrToDate(String dateStr) throws KmtcRuntimeException{
		
		if( dateStr.length() != 8 ){
			throw new KmtcRuntimeException("?��?��?�� String ?�� length ?�� 8?���?, 'yyyyMMdd' ?��?�� ?��?��?�� ?��?��?��.");
		}
		
		Calendar cal = Calendar.getInstance();
		cal.set(Integer.parseInt(dateStr.substring(0, 4)), Integer.parseInt(dateStr.substring(4,6))-1, Integer.parseInt(dateStr.substring(6,8)));
		
		return cal.getTime();
		
	}
	
	/**
	 * �??�� ?��?�� �??��?���? 
	 * @param today ?��?�� ?���? 
	 * @return Years String
	 */
	public static String getYears(String today){
		StringBuffer buff = new StringBuffer(1024);
		today = today.substring(0,4);
		int startday = Integer.parseInt(today)-2;
  	
		for( ; startday < Integer.parseInt(today)+3 ; startday++ ){
			buff.append("<option value='"+ startday +"' ");
			if( today.equals(""+startday) ) {
				buff.append("	selected ");  
			}
			buff.append(" > "+ startday +"</option>\n");	
		}
		return buff.toString();
	}
	
	/**
	 * �??�� ?�� �??��?���?
	 * @param today ?��?�� ?���? 
	 * @return Months String
	 */
	public static String getMonths(String today){
		StringBuffer buff = new StringBuffer(1024);
	
		for(int i=1; i<13; i++){
			if(i >= 10){
				buff.append("<option value='"+ i +"' ");
				if(( today.substring(4,6)).equals(""+i)) {
					buff.append(" selected ");  
				}	
			}
			else{
				buff.append("<option value='0"+ i +"' ");
				if(( today.substring(4,6)).equals("0"+i)) {
					buff.append(" selected ");  
				}	
			}	
			buff.append(" > "+ i +"</option>\n");
		}
		return buff.toString();
  	}
	
	/**
	 * �??�� ?�� �??��?���? 
	 * @param today ?��?�� ?���? 
	 * @return Days String
	 */
	public static String getDays(String today){
		StringBuffer buff = new StringBuffer(1024);
	
		for(int i=1; i<32; i++){
			if(i <= 9) {  
				buff.append("<option value='0"+ i +"' ");
				if(( today.substring(6,8)).equals("0"+i)) {
					buff.append(" 	selected ");  
				}
				buff.append(" > "+ i +"</option>\n");
			}
			else{
				buff.append("<option value='"+ i +"' ");
				if(( today.substring(6,8)).equals(""+i)) {
					buff.append(" 	selected ");  
				}
				buff.append(" > "+ i +" </option>\n");
			}
		}
		return buff.toString();
	}
	
	/**
	 * ?��?��짜의 ?�� ?�� 차이
	 * @param firstDay
	 * @param secondDay
	 * @return
	 */
	public static int getBetweenDays(String firstDay, String secondDay){
		
		if (firstDay.length()==8 && secondDay.length() == 8 ){
			Date firstDate = DateUtil.parseStrToDate(firstDay);
			Date secondDate = DateUtil.parseStrToDate(secondDay);
			
			Calendar cal = Calendar.getInstance();
			
			cal.setTime(firstDate);
			long s1 = cal.getTimeInMillis();
			
			cal.setTime(secondDate);
			long s2 = cal.getTimeInMillis();
			
			Long diffLong = new Long(s2-s1);
					
			return diffLong.intValue()/1000/60/60/24 + 1;
		}else {
			return 0;
		}
	}
	
}
